package com.hzqy.service.epg;

import com.hzqy.commons.utils.RedisUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName RedisService
 * @Author hff
 * @Date 2018/12/7 4:37 PM
 * @Version 1.0
 */
@Service
public class RedisService {
    private final static String POP_UTS = "PU";//用户弹窗记录数据前缀
    private final static String USER_LOGIN = "UL";//用户当天登录记录
    private final static String WCHAT_PARAMS = "WX";
    private final static String Reset_MESS="RM";//用户重置积分记录数据前缀
    private final static String AC_PLAYTIME="AC_PLAYTIME";//活动用户播放时间
    private final static String AC_PLAYTIME_UNIX="AC_PLAYTIME";//活动用户播放时间
    
    public static String getDay(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String dateStr = sdf.format(date);
        return dateStr;
    }
    public static long getTime(){
    	Date date = new Date();
		long nowTime =date.getTime();
        return nowTime;
    }
    public static String getMonth(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        String dateStr = sdf.format(date);
        return dateStr;
    }

    /**
      *@ClassName 获取三天后最后一秒时间戳，数据保留3天
      *@Date 2018/12/14 4:50 PM
      *@Author hff
      *@return 返回三天后最后一秒时间戳
      */
    public static Long getDayLastMis(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, 3);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date start = calendar.getTime();
        return start.getTime()/1000;
    }
    
    /**
     *@ClassName 返回几天最后一秒时间戳
     *@return 返回几天最后一秒时间戳
     */
    public static Long getTodayDayLastMis(int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, day);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date start = calendar.getTime();
        return start.getTime()/1000;
    }
    /**
      *@ClassName 获取下月5号最后一秒时间戳，数据保留5天
      *@Date 2018/12/14 4:51 PM
      *@Author hff
      *@return 返回下月5号最后一秒时间戳
      */
    public static Long getMonthLastMis(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, 5);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.SECOND, -1);
        Date start = calendar.getTime();
        return start.getTime()/1000;
    }

    /**
      *@ClassName 存储用户每天登录记录
      *@Date 2018/12/7 5:39 PM
      *@Author hff
      *@param key 键
      */
    public static void setUserLoginUnixOneDay(String key){
        RedisUtils.setStringUnix(USER_LOGIN+getDay(),key,"1",getDayLastMis());
    }

    /**
      *@ClassName 获取用户当天是否存在记录
      *@Date 2018/12/7 5:40 PM
      *@Author hff
      *@param key 键
      *@return 返回用户当天访问记录
      */
    public static String getStringData(String key){
        return RedisUtils.getString(USER_LOGIN+getDay(),key);
    }

    /**
      *@ClassName 存储用户当月弹窗记录，val自动获取
      *@Date 2018/12/7 5:41 PM
      *@Author hff
      *@param key 键
      */
    public static void setPopUtsUnixOneMonth(String key){
        RedisUtils.pushListUnix(POP_UTS+getMonth(),key,getDay(),getMonthLastMis());
    }

    /**
      *@ClassName 存储用户当月弹窗记录，val传入
      *@Date 2018/12/21 9:31 AM
      *@Author hff
      *@param key 键
      *@param val 值
      */
    public static void setPopUtsDataUnixOneMonth(String key, String val){
        RedisUtils.pushListUnix(POP_UTS+getMonth(),key,val,getMonthLastMis());
    }

    /**
      *@ClassName 获取用户当月弹窗记录
      *@Date 2018/12/7 5:41 PM
      *@Author hff
      *@param key 键
      *@return 返回用户弹窗记录
      */
    public static List<String> getListData(String key){
        return RedisUtils.getList(POP_UTS+getMonth(),key);
    }

    /**
     *@ClassName 存储用户微信AccessToken
     */
    public static void setAccessToken(String key, String val, int creatTime){
        RedisUtils.setStringCountdown(WCHAT_PARAMS,key,val,creatTime);
    }

    /**
     *@ClassName 获取用户微信AccessToken
     */
    public static String getAccessToken(String key){
        return RedisUtils.getString(WCHAT_PARAMS,key);
    }

    /**
      *@ClassName redis数据清理，生产网不使用
      *@Date 2018/12/11 12:25 PM
      *@Author hff
      */
    public static void cleanData(){
        //RedisUtils.cleanData();
    }
    /**
     * @ClassName 存入用户当天重置数据标志
     * @Date 2018/03/19
     * @Author LiuL
     * **/
    public static void setReset_info(String key){
        RedisUtils.setStringUnix(Reset_MESS+getDay(),key,"1",getDayLastMis());
    }
    /**
     * @ClassName 获取用户当天重置数据标志
     * @Date 2018/03/19
     * @Author LiuL
     * **/
    public static String getReset_info(String key){
        return RedisUtils.getString(Reset_MESS+getDay(),key);
    }
    /**
     *存储用户播放开始时间
     */
    public static void setPlayTime(String key,String val, int unixTime){
    	RedisUtils.setex(AC_PLAYTIME,key,val,unixTime);
    }
    /**
     *获取用户播放开始时间
     */
    public static String getPlayTimeString(String key){
    	return RedisUtils.getString( AC_PLAYTIME,key);
    }
    /**
     *清除数据
     */
    public static void removePlayTimeString(String key){
    	RedisUtils.remove(AC_PLAYTIME,key);
    }
}
